﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

namespace huaguwang
{
    public static class CatagroyViewList
    {
        public class NodeItem
        {
            #region 属性定义
            /// <summary>
            /// 子结点ID
            /// </summary>       
            public int ID { get; set; }
            /// <summary>
            /// 结点深度
            /// </summary>
            public int Depth { get; set; }
            /// <summary>
            /// 名称
            /// </summary>
            public string Title { get; set; }
            /// <summary>
            /// 父节点的ID
            /// </summary>
            public int ParentID { get; set; }
            #endregion
        }

        private const string  closestr= "/hgmanage/images/tree_open.gif";
        private const string  openstr= "/hgmanage/images/tree_close.gif";
        private const string notestr = "/hgmanage/images/tree_node.gif";//

        public static string BindTreeList(List<NodeItem> Items)
        {
            #region 前期准备

            var sb = new StringBuilder();
            var style = "<span class=\"sign\" style=\"margin-left:{0}px\"><img src=\"{1}\"/>\r\n</span>\r\n";
            var text = "";// ;
            var con = "<input type=\"radio\" value=\"{0}\" name=\"Cate\" />{1}";
            var divcon = "<div id=\"{0}\" class=\"pet_class\">\r\n{1}\r\n</div>";
            var stycon = "";
            var content = "";
            var curDepth = 0;
            var netDepth = 0;
            var depthList = new List<int>();

            #endregion
            #region 遍历数据
            for (int i = 0; i < Items.Count; i++)
            {
                #region 初始化层数据
                curDepth = Items[i].Depth;
                netDepth = (i < Items.Count - 1) ? Items[i + 1].Depth : 0;
                #endregion

                #region 处理带根结点 跳过
                if (curDepth == 0)
                {
                    continue;
                }
                #endregion

                #region 设置显示内容
                if (curDepth == 1)
                {
                    #region 第一层时 即最顶层情况
                    if (curDepth < netDepth)
                    {//下一层大于当前层
                        stycon = style.Exp_Format(0, openstr);
                    }
                    else
                    {//下一层等于当前层
                        stycon = style.Exp_Format(0, openstr);
                    }

                    if (Items.Count > 1)
                    {//内容不止一顶时的顶层
                        var str1 = "";
                        if (curDepth == netDepth)
                        {
                            str1 = Items[i].Title;
                        }
                        else
                        {
                            if (i < Items.Count - 1)
                            {
                                str1 = "<span class=\"ItemContent\">{0}</span>".Exp_Format(Items[i].Title);
                            }
                            else
                            {
                                str1 = Items[i].Title;
                            }
                        }
                        text = con.Exp_Format(Items[i].ID, str1);
                        content = divcon.Exp_Format("level_" + Items[i].ID, stycon + text);
                    }
                    else
                    {//内容就一顶时
                        var str1 = Items[i].Title;
                        text = con.Exp_Format(Items[i].ID, str1);
                        content = stycon + text;
                    }
                    #endregion
                }
                else
                {
                    #region 非顶层顶
                    if (i < Items.Count - 1)
                    {//非最后项
                        #region 非最后项
                        if (netDepth > curDepth)
                        {//下一层大于当前层
                            var str1 = "<span class=\"ItemContent\">{0}</span>".Exp_Format(Items[i].Title);
                            stycon = style.Exp_Format((Items[i].Depth - 1) * 20, openstr);//样式 
                            text = con.Exp_Format(Items[i].ID, str1);
                            content = divcon.Exp_Format("level_" + Items[i].ID, stycon + text);
                        }
                        else
                        {//下一层小于等于当前层
                            text = con.Exp_Format(Items[i].ID, Items[i].Title);
                            stycon = style.Exp_Format((Items[i].Depth - 1) * 20, notestr);//样式
                            content = stycon + text;
                        }
                        #endregion
                    }
                    else
                    {
                        #region 最后项
                        stycon = style.Exp_Format((Items[i].Depth - 1) * 20, notestr);//样式
                        var str1 = Items[i].Title;
                        text = con.Exp_Format(Items[i].ID, str1);
                        content = stycon + text;
                        #endregion
                    }
                    #endregion
                }
                #endregion

                #region 设置div标签
                if (i == 0)
                {
                    #region 第一项
                    sb.Append(content);
                    sb.Append("\r\n");
                    if (Items.Count > 1)
                    {
                        depthList.Add(curDepth);
                    }

                    if (curDepth < netDepth)
                    {
                        sb.Append("<div id=\"level_" + Items[i].ID + "_son\" class=\"sub_class on\" >\r\n");
                    }

                    #endregion
                }
                else
                {
                    #region 第二项到 倒数 最后项
                    if (i == Items.Count - 1)
                    { //到数第一项
                        #region 最后项
                        sb.Append(content);
                        sb.Append("\r\n");

                        for (var k = 0; k < depthList.Count; k++)
                        {
                            sb.Append("</div>\r\n");
                        }
                        #endregion
                    }
                    else
                    {
                        #region 第二项到 倒数第二项
                        //当前项的层大于depth的最后项  当前项为 depth中最后项的子项 则添加div
                        if (netDepth > curDepth)
                        {//当前层大于上一最大层号 先添加div，再添加内容和list
                            #region 当前层小于下一层
                            sb.Append(content);
                            if (content.IndexOf("</div>") > 0)
                            {
                                sb.Append("\r\n");
                            }
                            else
                            {
                                sb.Append("<br/>\r\n");
                            }
                            sb.Append("<div id=\"level_" + Items[i].ID + "_son\" class=\"sub_class on\" >\r\n");
                            depthList.Add(curDepth);
                            #endregion
                        }
                        else
                        {
                            #region 当前层不小于下一层
                            if (netDepth < curDepth)
                            {//当前层小于上一最大层号
                                #region 当前层大于下一层
                                if (depthList.Count > 0)
                                {
                                    depthList.RemoveAt(depthList.Count - 1);
                                }
                                sb.Append(content);

                                for (int k = 0; k < curDepth - netDepth; k++)
                                {
                                    sb.Append("\r\n</div>\r\n");
                                    if (depthList.Count > 0)
                                    {
                                        depthList.RemoveAt(depthList.Count - 1);
                                    }
                                }

                                #endregion
                            }
                            else
                            {//当前层等于上一最大层号
                                #region 当前层等于下一层
                                sb.Append(content);

                                if (curDepth > 1)
                                {
                                    sb.Append("<br/>\r\n");
                                }
                                else
                                {
                                    if (content.IndexOf("</div>") > 0)
                                    {
                                        sb.Append("\r\n");
                                    }
                                    else
                                    {
                                        sb.Append("<br/>\r\n");
                                    }
                                }
                                #endregion
                            }
                            #endregion
                        }
                        #endregion
                    }
                    #endregion
                }
                #endregion
            }
            #endregion
            return sb.ToString();
        }
    }
}
